package com.gitee.flyzing.jxt.component;

import com.gitee.flyzing.jxt.CommonUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;

import java.util.Map;

public class JXTCell {
    private Object cellValue;
    private transient CellType cellType;
    private transient CellStyle cellStyle;
    private String cellFormula;

    public static JXTCell createJXTCell(Cell cell, Object datas, Map params) {
        String itemName = params != null ? params.get("itemName").toString() : ""; //each item 变量名字
        String cellValue = getXSSFCellValue(cell).toString();//解析值时用全部使用String
        JXTCell jxtCell = null;
        //以{{开头为jxt表达式
        if (cellValue.startsWith("{{")) {
            //each表达式
            if (cellValue.indexOf("jxt.each") > -1) { //each表达式
                jxtCell = new JXTEachCell();
                jxtCell.parseCell(cell, datas);
            } else if(CommonUtil.isNotNull(itemName) && cellValue.indexOf(itemName + ".") > -1) { //each item取值表达式
                jxtCell = new JXTEachItemCell();
                jxtCell.parseCell(cell, datas, params);
            }else { //求值表达式
                jxtCell = new JXTValueCell();
                jxtCell.parseCell(cell, datas);
            }
        } else  {//常量
            jxtCell = new JXTCell();
            jxtCell.setCellValue(cellValue);
        }

        jxtCell.setCellType(cell.getCellType());
        jxtCell.setCellStyle(cell.getCellStyle());
        if (cell.getCellType() == CellType.FORMULA) {
            String cellFormula = cell.getCellFormula();
            jxtCell.setCellFormula(cellFormula);
        }
        return jxtCell;
    }

    /**
     * 子类自定义单元格解析规则
     * @param xssfCell
     * @param datas
     */
    public void parseCell(Cell xssfCell, Object datas) {}
    public void parseCell(Cell xssfCell, Object datas, Map params) {}

    public static Object getXSSFCellValue(Cell xssfCell) {
        Object cellValue = null;
        CellType cellType = xssfCell.getCellType();
        switch (cellType) {
            case NUMERIC:
            case FORMULA:
                try {
                    cellValue = xssfCell.getNumericCellValue();
                } catch (IllegalStateException e) {
                    cellValue = xssfCell.getRichStringCellValue();
                }
                break;
            case BLANK:
            case STRING:
                cellValue = xssfCell.getStringCellValue();
                break;
            default:
                throw new RuntimeException("暂不支持单元格类型" + cellType);
        }
        return cellValue;
    }

    public void createXSSFCell(XSSFRow xssfRow, int row, int col) {
        XSSFCell xssfCell = xssfRow.createCell(col);
        switch (this.cellType) {
            case NUMERIC:
                xssfCell.setCellType(cellType);
            case FORMULA:
                xssfCell.setCellFormula(cellFormula); //公式不能设置cellType
                xssfCell.setCellValue(Double.parseDouble(cellValue.toString()));
                break;
            case BLANK:
            case STRING:
                xssfCell.setCellType(cellType);
                xssfCell.setCellValue(cellValue.toString());
                break;
            default:
                throw new RuntimeException("暂不支持单元格类型" + cellType);
        }

        CellStyle style = xssfRow.getSheet().getWorkbook().createCellStyle();
        style.cloneStyleFrom(cellStyle);    // 克隆出一个style
        xssfCell.setCellStyle(style);
    }

    public Object getCellValue() {
        return cellValue;
    }

    public void setCellValue(Object cellValue) {
        this.cellValue = cellValue;
    }

    public CellType getCellType() {
        return cellType;
    }

    public void setCellType(CellType cellType) {
        this.cellType = cellType;
    }

    public CellStyle getCellStyle() {
        return cellStyle;
    }

    public void setCellStyle(CellStyle cellStyle) {
        this.cellStyle = cellStyle;
    }

    public String getCellFormula() {
        return cellFormula;
    }

    public void setCellFormula(String cellFormula) {
        this.cellFormula = cellFormula;
    }
}
